package com.zsr.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author zsr
 * @desc 返回的参数封装类
 * @date 2021/6/4 11:13
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class APIResponse<T> implements Serializable {

    private static final long serialVersionUID = -6271258670391360980L;

    private boolean head;
    private int status;
    private String message;
    private T data;


    public static final boolean HEAD_SUCCESS = true;
    public static final boolean HEAD_FAILURE = false;
    private static final int CODE_SUCCESS = 200;
    private static final int CODE_FAIL = 500;

    public APIResponse(int status) {
        this.status = status;
    }

    public static APIResponse build(boolean head, int status, Object data, String message) {
        return new APIResponse(head, status, message, data);
    }

    public static APIResponse success() {
        return build(HEAD_SUCCESS, CODE_SUCCESS, null, "");
    }

    public static APIResponse success(Object data, String message) {
        return build(HEAD_SUCCESS, CODE_SUCCESS, data, message);
    }

    public static APIResponse success(Object data) {
        return build(HEAD_SUCCESS, CODE_SUCCESS, data, "");
    }

    public static APIResponse fail() {
        return build(HEAD_FAILURE, CODE_FAIL, "", "");
    }

    public static APIResponse fail(int status, String message) {
        return build(HEAD_FAILURE, status, "", message);
    }

    public static APIResponse fail(String message) {
        return build(HEAD_FAILURE, CODE_FAIL, "", message);
    }

    public static APIResponse widthCode(int status) {
        return new APIResponse(status);
    }


}
